package extension.game.zone;

import com.smartfoxserver.v2.api.CreateRoomSettings;
import com.smartfoxserver.v2.api.CreateRoomSettings.RoomExtensionSettings;
import com.smartfoxserver.v2.api.ISFSApi;
import com.smartfoxserver.v2.core.SFSEventType;
import com.smartfoxserver.v2.entities.Room;
import com.smartfoxserver.v2.exceptions.SFSCreateRoomException;
import com.smartfoxserver.v2.extensions.SFSExtension;
import log.Debug;

/**
 *
 * @author daz
 */
public class PokerZoneExtension extends SFSExtension {

    public static String[] GROUP_LIST = {RoomGroup.LEVEL1, RoomGroup.LEVEL2, RoomGroup.LEVEL3,
        RoomGroup.LEVEL4, RoomGroup.LEVEL5, RoomGroup.LEVEL6,
        RoomGroup.LEVEL7, RoomGroup.LEVEL8, RoomGroup.LEVEL9,
        RoomGroup.LEVEL10};
    private Response response;
    private int numberTablePerRooms = 10;
    private int numberTables = 1;

    @Override
    public void init() {
        Debug.setExt(this);
        response = new Response(this);
        Debug.i("INIT POKER ZONE");
        if (getParentZone().getRoomList().size() == 1) {
            for (int i = 0; i < numberTables; i++) {
                makeTable(i + 1, GROUP_LIST[i / numberTablePerRooms]);
            }
        }
        addEventHandler(SFSEventType.USER_JOIN_ROOM, JoinRoomHandler.class);
        addEventHandler(SFSEventType.USER_LOGIN, LoginEventHandler.class);
        addRequestHandler(Command.LIST_TABLE, ListTableInfoHandler.class);
        addRequestHandler(Command.TABLE_INFO, TableInfoHandler.class);
    }

    private void makeTable(int tableName, String groupId) {
        CreateRoomSettings settings = new CreateRoomSettings();
        settings.setName("R" + String.valueOf(tableName));
        settings.setMaxUsers(20);
        settings.setGame(true);
        settings.setGroupId(groupId);
        RoomExtensionSettings extensionSetting = new CreateRoomSettings.RoomExtensionSettings("PokerZone", "extension.game.poker.PokerRoomExtension");
        settings.setExtension(extensionSetting);
        ISFSApi api = getApi();
        try {
            Room room = api.createRoom(getParentZone(), settings, null, false, null, false, true);
        } catch (SFSCreateRoomException ex) {
            Debug.console("error :" + ex.toString());
        }
    }

    public Response getResponse() {
        return response;
    }
}
